﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicSplit101
{
    public class FinishEventArgs : EventArgs
    {
        public bool Successed { get; set; }
        public string Message { get; set; }
        public FinishEventArgs()
        {

        }
    }

    /// <summary>
    /// 坏品检测
    /// </summary>
    public partial class UCBadProductLearning : UserControl
    {
        public event EventHandler<FinishEventArgs> Finished;


        private int pid = 1;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }

        private int cid = 1;

        public int Cid
        {
            get { return cid; }
            set { cid = value; }
        }

        private int splitCount = 6; //默认切割次数设为6

        public int SplitCount
        {
            get { return splitCount; }
            set { splitCount = value; }
        }

        public UCBadProductLearning()
        {
            InitializeComponent();
            lblLayer.Text = "当前检测层数：" + SplitCount.ToString();
        }

        private void btnBadProductLearning_Click(object sender, EventArgs e)
        {
            Study("1");
        }

        public void Study(string key)
        {
            FinishEventArgs e = new FinishEventArgs() { Successed = true };
            try
            {
                btnBadProductLearning.Enabled = false;
                int count = Convert.ToInt32(DBUtils.GetUniqueValue("select count(1) from InspectionOption where [pid]=" + Pid ));
                if (count < 1)
                {
                    MessageBox.Show("请先设置检测层数！");
                    e.Message = "";
                    e.Successed = false;
                }
                else
                {
                    if (SplitCount + 1 > 13)
                    {

                        MessageBox.Show("检测层数最多为13层！");
                        e.Successed = false;
                    }
                    else
                    {
                        SplitCount++;
                        lblLayer.Text = "当前检测层数：" + SplitCount.ToString();
                        DBUtils.ExecuteSql("update InspectionOption set [Layer]=" + SplitCount + " where [pid]=" + Pid);
                        MessageBox.Show("坏品学习成功！");
                        e.Successed = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误：" + ex.Message + "\n" + ex.StackTrace);
                e.Successed = false;
            }
            finally
            {
                btnBadProductLearning.Enabled = true;
            }

            var temp = this.Finished;
            if (temp != null)
            {
                temp(this, e);
            }

        }
    }
}
